package com.furns.web;

import com.furns.entity.Cart;
import com.furns.entity.CartItem;
import com.furns.entity.Furn;
import com.furns.service.FurnService;
import com.furns.service.impl.FurnServiceImpl;
import com.furns.utils.DataUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author LuoJiaCheng
 * @ID 2240129126
 * @Version 1.0
 */
@WebServlet(name = "CartServlet", value = "/CartServlet")
public class CartServlet extends BasicServlet {
    private FurnService furnService = new FurnServiceImpl();


    //添加家居到购物车
    protected void addItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //添加家居的id
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        //获取家居信息
        Furn furn = furnService.queryFurnById(id);

        CartItem cartItem = new CartItem(furn.getId(), furn.getName(), furn.getPrice(), 1, furn.getPrice());

        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        //将cartItem加入到cart
        cart.addItem(cartItem);
        //添加完毕，返回添加家居的页面
        String referer = req.getHeader("Referer");
        resp.sendRedirect(referer);
    }

    protected void addItemByAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //添加家居的id
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        //获取家居信息
        Furn furn = furnService.queryFurnById(id);

        CartItem cartItem = new CartItem(furn.getId(), furn.getName(), furn.getPrice(), 1, furn.getPrice());

        Cart cart = (Cart) req.getSession().getAttribute("cart");
        if (cart == null) {
            cart = new Cart();
            req.getSession().setAttribute("cart", cart);
        }
        //将cartItem加入到cart
        cart.addItem(cartItem);
        //添加完毕，返回添加家居的页面
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("cartTotalCount",cart.getTotalCount());
        String cartTotalCount = new Gson().toJson(resultMap);
        resp.getWriter().write(cartTotalCount);
    }

    //更新某个CartItem的数量，即更新购物车
    protected void updateCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        int count = DataUtils.parseInt(req.getParameter("count"), 1);
            Cart cart =(Cart) req.getSession().getAttribute("cart");
            if(cart!=null){
                cart.updateCount(id,count);
            }
             //回到更新购物车的页面
            resp.sendRedirect(req.getHeader("Referer"));
    }

    /**
     * 删除 购物车其中一项
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delItem(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = DataUtils.parseInt(req.getParameter("id"), 0);
        Cart cart =(Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.delItem(id);
        }
        //回到更新购物车的页面
        resp.sendRedirect(req.getHeader("Referer"));
    }

    /**
     * 清空购物车
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void clear(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cart cart =(Cart) req.getSession().getAttribute("cart");
        if(cart!=null){
            cart.clear();
        }
        //回到更新购物车的页面
        resp.sendRedirect(req.getHeader("Referer"));
    }
}
